-- See https://www.lazyvim.org/plugins/lsp#%EF%B8%8F-customizing-lsp-keymaps

local spawn_spec = function(new_opts)
  return {
    "neovim/nvim-lspconfig",
    event = "LazyFile",
    dependencies = {
      "mason.nvim",
      { "mason-org/mason-lspconfig.nvim", config = function() end },
    },
    opts = new_opts,
  }
end

-- Clangd Config
local spec = spawn_spec({
  servers = {
    clangd = {
      cmd = {
        "clangd",
        -- 在后台自动分析文件（基于complie_commands)
        "--background-index",
        -- 标记compelie_commands.json文件的目录位置
        "--compile-commands-dir=build",
        -- 同时开启的任务数量
        "-j=12",
        -- clang-tidy功能
        "--clang-tidy",
        -- 全局补全（会自动补充头文件）
        "--all-scopes-completion",
        -- 更详细的补全内容
        "--completion-style=detailed",
        -- 补充头文件的形式
        "--header-insertion=iwyu",
        -- pch优化的位置
        "--pch-storage=memory",
      },
    },
    -- Lua
    lua_ls = {
      -- lazy-load schemastore when needed
      on_new_config = function(new_config)
        vim.bo.shiftwidth = 2
        vim.bo.tabstop = 2
      end,
    },
    -- Vim
    vimls = {
      -- lazy-load schemastore when needed
      on_new_config = function(new_config)
        vim.bo.shiftwidth = 2
        vim.bo.tabstop = 2
      end,
    },
    -- Json
    jsonls = {
      -- lazy-load schemastore when needed
      on_new_config = function(new_config)
        new_config.settings.json.schemas = new_config.settings.json.schemas or {}
        vim.list_extend(new_config.settings.json.schemas, require("schemastore").json.schemas())
        vim.bo.shiftwidth = 2
        vim.bo.tabstop = 2
      end,
      format = {
        enable = true,
        indent = 2,
      },
      validate = {
        enable = true,
      },
    },
    yamlls = {
      -- lazy-load schemastore when needed
      on_new_config = function(new_config)
        new_config.settings.yaml.schemas = new_config.settings.yaml.schemas or {}
        vim.list_extend(new_config.settings.yaml.schemas, require("schemastore").yaml.schemas())
        vim.bo.shiftwidth = 2
        vim.bo.tabstop = 2
      end,
    },
    -- JavaScript & TypeScript
    vtsls = {
      -- lazy-load schemastore when needed
      on_new_config = function(new_config)
        vim.bo.shiftwidth = 2
        vim.bo.tabstop = 2
      end,
    },
    -- Vue
    volar = {
      -- lazy-load schemastore when needed
      on_new_config = function(new_config)
        vim.bo.shiftwidth = 2
        vim.bo.tabstop = 2
      end,
    },
  },
})

local spec_nil = spawn_spec({
  servers = {
    -- Nix
    nil_ls = {
      settings = {
        ["nil"] = {
          formatting = {
            command = { "alejandra" }, -- 指定格式化命令
          },
        },
      },
      -- 设置文件类型关联（如果未自动检测）
      filetypes = { "nix" },
      -- lazy-load schemastore when needed
      on_new_config = function(new_config)
        vim.bo.shiftwidth = 2
        vim.bo.tabstop = 2
      end,
    },
  },
})

if vim.fn.has("win32") == 1 then
  return { spec }
else
  return { spec, spec_nil }
end
